package javaLearning.异步任务;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author kkmytery
 * @description TaskDemo
 * @date 2020/12/16 11:25 上午
 */
public class TaskDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();

        CompletableFuture<String> future1=CompletableFuture.supplyAsync(() -> {
            //task content
            System.out.println("this is lambda supplyAsync");
            System.out.println("future1是否为守护线程: "+Thread.currentThread().isDaemon());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("this lambda is executed by forkJoinPool");
            return "result1";
        });

        CompletableFuture<String> future2=CompletableFuture.supplyAsync(() -> {
            //task content
            System.out.println("this is lambda supplyAsync");
            System.out.println("future2是否为守护线程: "+Thread.currentThread().isDaemon());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("this lambda is executed by executorService");
            return "result2";
        },executorService);

        System.out.println(future1.get());
        System.out.println(future2.get());
        executorService.shutdown();
    }
}
